package ThreeLayer.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ThreeLayer.DAL.UserDA;
import TwoLayer.User;

public class ui extends JFrame {

    /**
     * 界面设计
     **/
    public void w1() {//添加功能实现
        JFrame jFrame = new JFrame("添加用户");
        jFrame.setBounds(400, 400, 300, 400);
        JLabel j1 = new JLabel("请输入序号");
        JTextField textField1 = new JTextField(20);
        textField1.setText("");
        JLabel j2 = new JLabel("请输入姓名");
        JTextField textField2 = new JTextField(20);
        textField2.setText("");
        JLabel j3 = new JLabel("请输入电话");
        JTextField textField3 = new JTextField(20);
        textField3.setText("");
        JLabel j4 = new JLabel("请输入住址");
        JTextField textField4 = new JTextField(20);
        textField4.setText("");
        JPanel panel1 = new JPanel(new GridLayout(8, 1));
        panel1.add(j1);
        panel1.add(textField1);
        panel1.add(j2);
        panel1.add(textField2);
        panel1.add(j3);
        panel1.add(textField3);
        panel1.add(j4);
        panel1.add(textField4);
        JButton button = new JButton("提交数据库");
        JPanel panel = new JPanel();
        panel.add(button);
        jFrame.add(panel1);

        jFrame.add(panel, BorderLayout.SOUTH);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserDA userDA = new UserDA();
                User user = new User();
                int f = 0;
                int i = Integer.parseInt(textField1.getText());
                user.setId(i);
                user.setUsername(textField2.getText());
                user.setPhone(textField3.getText());
                user.setAddress(textField4.getText());
                try {
                    userDA.addUser(user);
                    f = 1;
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                if (f == 1) {
                    JOptionPane.showMessageDialog(jFrame, "数据添加成功!");
                } else if (f == 0) {
                    JOptionPane.showMessageDialog(jFrame, "数据添加失败!");
                }
            }
        });
        jFrame.setResizable(false);
        jFrame.setVisible(true);
    }

    public void w2(){//删除功能实现
        JFrame jFrame = new JFrame("删除用户");
        jFrame.setBounds(400, 400, 300, 400);
        JLabel j1 = new JLabel("请输入序号");
        JTextField textField1 = new JTextField(10);
        textField1.setText("");
        JPanel panel1 = new JPanel(new GridLayout(8, 1));
        panel1.add(j1);
        panel1.add(textField1);
        JButton button = new JButton("确认删除");
        JPanel panel = new JPanel();
        panel.add(button);
        jFrame.add(panel1);

        jFrame.add(panel, BorderLayout.SOUTH);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserDA userDA = new UserDA();
                User user = new User();
                int f = 0;
                int i = Integer.parseInt(textField1.getText());
                user.setId(i);
                try {
                    userDA.delUser(user);
                    f = 1;
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                if (f == 1) {
                    JOptionPane.showMessageDialog(jFrame, "数据添加成功!");
                } else if (f == 0) {
                    JOptionPane.showMessageDialog(jFrame, "数据添加失败!");
                }
            }
        });
        jFrame.setResizable(false);
        jFrame.setVisible(true);
    }

    public void w3(){
        JFrame jFrame = new JFrame("修改用户");
        jFrame.setBounds(400, 400, 300, 400);
        JLabel j1 = new JLabel("请输入序号");
        JTextField textField1 = new JTextField(20);
        textField1.setText("");
        JLabel j2 = new JLabel("请输入姓名");
        JTextField textField2 = new JTextField(20);
        textField2.setText("");
        JLabel j3 = new JLabel("请输入电话");
        JTextField textField3 = new JTextField(20);
        textField3.setText("");
        JLabel j4 = new JLabel("请输入住址");
        JTextField textField4 = new JTextField(20);
        textField4.setText("");
        JPanel panel1 = new JPanel(new GridLayout(8, 1));
        panel1.add(j1);
        panel1.add(textField1);
        panel1.add(j2);
        panel1.add(textField2);
        panel1.add(j3);
        panel1.add(textField3);
        panel1.add(j4);
        panel1.add(textField4);
        JButton button = new JButton("提交数据库");
        JPanel panel = new JPanel();
        panel.add(button);
        jFrame.add(panel1);

        jFrame.add(panel, BorderLayout.SOUTH);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserDA userDA = new UserDA();
                User user = new User();
                int f = 0;
                int i = Integer.parseInt(textField1.getText());
                user.setId(i);
                user.setUsername(textField2.getText());
                user.setPhone(textField3.getText());
                user.setAddress(textField4.getText());
                try {
                    userDA.updateUser(user);
                    f = 1;
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                if (f == 1) {
                    JOptionPane.showMessageDialog(jFrame, "数据修改成功!");
                } else if (f == 0) {
                    JOptionPane.showMessageDialog(jFrame, "数据修改失败!");
                }
            }
        });
        jFrame.setResizable(false);
        jFrame.setVisible(true);
    }

    public void w4(){
        JFrame jFrame = new JFrame("查看用户");
        jFrame.setBounds(400, 400, 400, 300);
        TextArea t1 = new TextArea(20,20);
        t1.setBackground(Color.green);
        JPanel panel1 = new JPanel(new GridLayout(1, 8));
        panel1.add(t1);
        JButton button = new JButton("查看全部联系人");
        JPanel panel = new JPanel();
        panel.add(button);
        jFrame.add(panel1);

        jFrame.add(panel, BorderLayout.SOUTH);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<User> list = null;
                UserDA userDA = new UserDA();
                try {
                    list =  userDA.showUser();
                    for (User user : list) {
                        t1.append(user.toString().replace("User{", "").replace("id", "序号")
                                .replace("Username", "姓名").replace("address", "地址").replace("phone", "电话").replace("'", "")
                                .replace("'", "").replace("}", "").replace("=", ":  ") + "\n");
                    }
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        jFrame.setResizable(false);
        jFrame.setVisible(true);
    }
    public ui() {

        Container c = getContentPane(); //定义一个顶级容器c

        JPanel jp = new JPanel(); //新建JPanel面板--jp

        JButton button1 = new JButton("新建联系人");

        JButton button2 = new JButton("删除联系人");

        JButton button3 = new JButton("编辑联系人");

        JButton button4 = new JButton("显示所有联系人");


        jp.setLayout(new GridLayout(2, 4, 5, 5));//新建网格布局管理器(行数，列数，组件间的水平垂直间距)

        jp.add(button1);

        jp.add(button2);

        jp.add(button3);

        jp.add(button4);


        c.add(jp);//将JPanel面板jp添加到顶级容器c中

        setSize(600, 500);

        setTitle("*通 讯 录 管 理 系 统*");

        setVisible(true);

        setResizable(false);//窗体大小由程序员决定，用户不能自由改变大小

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

/**
 *按键响应
 *
 **/

        button1.addActionListener(new ActionListener() {//添加功能实现

            @Override
            public void actionPerformed(ActionEvent arg0) {w1();}
        });



        button2.addActionListener(new ActionListener() {//删除功能实现

            @Override
            public void actionPerformed(ActionEvent arg0) {w2();}
        });

        button3.addActionListener(new ActionListener() {//修改功能实现

            @Override
            public void actionPerformed(ActionEvent arg0) {w3();}
        });



        button4.addActionListener(new ActionListener() {//查询功能实现

            @Override
            public void actionPerformed(ActionEvent arg0) {w4();}
        });


    }
    public static void main(String[] args){
        new ui();
    }
}


